import logging.handlers
import os
from config import BASE_PATH


# 日志封装
class GetLog:
    __log = None

    @classmethod
    def get_log(cls,log_open):
        if cls.__log is None:
            # 获取日志器
            cls.__log = logging.getLogger()
            # 设置入口级别
            if log_open == False:
                # 不打印日志
                cls.__log.setLevel(logging.CRITICAL + 1)
            else:
                # 打印日志
                cls.__log.setLevel(logging.INFO)
            # 获取处理器
            filename = BASE_PATH + os.sep + "log" + os.sep + "okcloud.log"
            tf = logging.handlers.TimedRotatingFileHandler(filename=filename,
                                                           when="midnight",
                                                           interval=1,
                                                           backupCount=3,
                                                           encoding="utf-8")
            # 获取格式器
            fmt = "%(asctime)s %(levelname)s [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s"
            fm = logging.Formatter(fmt)
            # 将格式器添加到处理器
            tf.setFormatter(fm)
            # 将处理器添加到日志器
            cls.__log.addHandler(tf)
        # 返回日志器
        return cls.__log

if __name__ == "__main__":
    pass